'''
Created on 07/08/2009

@author: eh01
'''
from package import build
from xml.etree import ElementTree
import base64
import unittest
from datastore import *

class BuildTest(unittest.TestCase):


    def setUp(self):
        pass


    def tearDown(self):
        pass


    def test_get_modules_case_1(self):
        '''[boot] test get modules with a syntactically correct manifest'''
        xml_string = '''<?xml version="1.0" encoding="UTF-8" ?>
        <resource>
            <content>
                <list key="package:manifest/modules">
                    <dict>
                        <item key="package:filename" type="yarra:type/string">module1.py</item>
                        <item key="package:startup" type="yarra:type/boolean">True</item>
                    </dict>
                    <dict>
                        <item key="package:filename" type="yarra:type/string">module2.py</item>
                        <item key="package:startup" type="yarra:type/boolean">False</item>
                    </dict>            
                    <dict>
                        <item key="package:filename" type="yarra:type/string">module3.py</item>
                        <item key="package:startup" type="yarra:type/boolean">True</item>
                    </dict>                                
                </list>
            </content>
        </resource>''' 
        manifest_xml_root_element = ElementTree.XML(xml_string)
        self.assertEqual({'module1.py':True, 'module2.py':False, 'module3.py':True}, 
                         build.get_modules(Resource(xml_string)))

    def test_get_modules_case_2(self):
        '''[boot] test get modules with a syntactically correct manifest but no modules'''
        xml_string = '''<?xml version="1.0" encoding="UTF-8" ?>
        <resource>
            <content>
                <list key="package:manifest/modules">                              
                </list>
            </content>
        </resource>''' 
        
        manifest_xml_root_element = ElementTree.XML(xml_string)
        self.assertEqual({}, build.get_modules(Resource(xml_string)))        

    def test_get_module_case_3(self):
        '''[boot] test get modules with a syntactically incorrect manifest'''
        xml_string = '''<?xml version="1.0" encoding="UTF-8" ?>
        <resource>
            <blahtag>
                <list key="package:manifest/modules">
                    <dict>
                        <item key="package:filename" type="yarra:type/string">module1.py</item>
                        <item key="package:startup" type="yarra:type/boolean">True</item>
                    </dict>
                    <dict>
                        <item key="package:filename" type="yarra:type/string">module2.py</item>
                        <item key="package:startup" type="yarra:type/boolean">False</item>
                    </dict>            
                    <dict>
                        <item key="package:filename" type="yarra:type/string">module3.py</item>
                        <item key="package:startup" type="yarra:type/boolean">True</item>
                    </dict>                                
                </list>
            </blahtag>
        </resource>''' 
        
        manifest_xml_root_element = ElementTree.XML(xml_string)
        try:
            build.get_modules(Resource(xml_string))
            self.fail('Should not be able to parse the xml')
        except DeserialisationException:
            assert True

    def test_create_resource_from_file_case_1(self):
        '''[build] test create_resource_from_file where file is not a serialised resource and not startup'''
        modules_in_package = {'module1.py':True, 'module2.py':False, 'module3.py':True}
        file_name = 'module2.py'
        content = 'yaya'
        package_name = 'dummypackage'
        rs = build.create_resource_from_file(file_name, content, package_name, modules_in_package)
        self.assertEqual(rs.content, base64.b64encode(content))
        self.assertEqual(rs.get_single_metadata(build.METADATA_PACKAGE_NAME), package_name)
        self.assertEqual(rs.get_single_metadata(build.METADATA_FILE_NAME), file_name)
        self.assertEqual(rs.get_single_metadata(build.METADATA_STARTUP), False)
        self.assertEqual(rs.get_single_metadata(build.METADATA_MODULE_NAME), package_name + '.' + file_name.strip('.py'))
        self.assertEqual(rs.get_single_metadata(build.METADATA_RESOURCE_TYPE), build.METADATA_MODULE)

    def test_create_resource_from_file_case_2(self):
        '''[build] test create_resource_from_file where file is not a serialised resource and startup'''
        modules_in_package = {'module1.py':True, 'module2.py':False, 'module3.py':True}
        file_name = 'module1.py'
        content = 'yaya'
        package_name = 'dummypackage'
        rs = build.create_resource_from_file(file_name, content, package_name, modules_in_package)
        self.assertEqual(rs.content, base64.b64encode(content))
        self.assertEqual(rs.get_single_metadata(build.METADATA_PACKAGE_NAME), package_name)
        self.assertEqual(rs.get_single_metadata(build.METADATA_FILE_NAME), file_name)
        self.assertEqual(rs.get_single_metadata(build.METADATA_STARTUP), True)
        self.assertEqual(rs.get_single_metadata(build.METADATA_MODULE_NAME), package_name + '.' + file_name.strip('.py'))
        self.assertEqual(rs.get_single_metadata(build.METADATA_RESOURCE_TYPE), build.METADATA_MODULE)        
        
    def test_create_resource_from_file_case_3(self):
        '''[build] test create_resource_from_file where file is an serialised resource'''
        modules_in_package = {'module1.py':True, 'module2.py':False, 'module3.py':True}
        file_name = 'blah.xml'
        content = '''<content><list key="package:manifest/modules" /></content>'''
        xml = '''<?xml version="1.0" encoding="UTF-8" ?><resource>{0}</resource>'''.format(content)
        package_name = 'dummypackage'
        rs = build.create_resource_from_file(file_name, xml, package_name, modules_in_package)
        self.assertEqual(rs.get_single_metadata(build.METADATA_PACKAGE_NAME), package_name)
        self.assertEqual(rs.get_single_metadata(build.METADATA_FILE_NAME), file_name)
        self.assertRaises(IndexError, rs.get_single_metadata, build.METADATA_STARTUP)
        self.assertRaises(IndexError, rs.get_single_metadata, build.METADATA_MODULE_NAME)
        self.assertRaises(IndexError, rs.get_single_metadata, build.METADATA_RESOURCE_TYPE)    
        
    def test_create_resource_from_file_case_4(self):
        '''[build] test create_resource_from_file where file is empty'''
        modules_in_package = {'module1.py':True, 'module2.py':False, 'module3.py':True}
        file_name = 'module2.py'
        package_name = 'dummypackage'
        content = ''
        rs = build.create_resource_from_file(file_name, content, package_name, modules_in_package)
        self.assertEqual(rs, None)
        content = None
        rs = build.create_resource_from_file(file_name, content, package_name, modules_in_package)
        self.assertEqual(rs, None)
        content = '    '
        rs = build.create_resource_from_file(file_name, content, package_name, modules_in_package)
        self.assertEqual(rs, None)                          
        
        
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()